home *** CD-ROM | disk | FTP | other *** search
- Path: news.production.compuserve.com!news
- From: Dave Hand <70621.3624@CompuServe.COM>
- Newsgroups: comp.lang.c++
- Subject: Re: Integer SQRT(), HELP!
- Date: 2 Jan 1996 06:44:54 GMT
- Organization: Singular Software, Inc.
- Message-ID: <4cak96$f9k$1@mhadg.production.compuserve.com>
- References: <4b63tm$al9@alpha.ocbbs.gen.nz>
-
- Here is an algorithm that works. I'm not sure if it is optimal or
- even faster than just converting your int to a double and using the
- standard sqrt() that is on the coprocessor. I only used when I was
- doing work on a 286 without a coprocessor.
-
- //
- // Find the square root of an integer.
- //
- unsigned long square_root(unsigned long s)
- {
- unsigned long a,prev;
-
- prev = a = (s/5 + 2);
- while(1)
- {
- a = (s/a + a) >> 1;
- if(abs(a - prev) <= 1) break;
- prev = a;
- }
- return(a);
- }
-
-